<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理系统 - 任务详情</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('dashboard') }}">
                <i class="fa fa-tasks mr-2"></i>任务管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('dashboard') }}">
                            <i class="fa fa-dashboard mr-1"></i>仪表盘
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('task_list') }}">
                            <i class="fa fa-list mr-1"></i>任务列表
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('category_list') }}">
                            <i class="fa fa-folder mr-1"></i>分类管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">
                            <i class="fa fa-bar-chart mr-1"></i>数据报表
                        </a>
                    </li>
                </ul>
                <div class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fa fa-user-circle mr-1"></i>{{ session.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('notifications') }}">
                                    <i class="fa fa-bell mr-1"></i>通知
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('settings') }}">
                                    <i class="fa fa-cog mr-1"></i>设置
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('logout') }}">
                                    <i class="fa fa-sign-out mr-1"></i>退出登录
                                </a>
                            </li>
                        </ul>
                    </li>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="container-fluid mt-4">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <!-- 页面标题和操作按钮 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2>
                    <i class="fa fa-tasks mr-2"></i>{{ task.title }}
                </h2>
                <div class="d-flex gap-2 mt-1">
                    {% if task.category %}
                        <span class="badge" style="background-color: {{ task.category.color }}">{{ task.category.name }}</span>
                    {% endif %}
                    <span class="badge bg-{{ 'danger' if task.priority == 'high' else 'warning' if task.priority == 'medium' else 'success' }}">
                        {{ task.priority | capitalize }}
                    </span>
                    <span class="badge bg-{{ 'secondary' if task.status == 'todo' else 'info' if task.status == 'in_progress' else 'success' if task.status == 'completed' else 'dark' }}">
                        {{ task.status | capitalize }}
                    </span>
                    {% if task.tags %}
                        {% for tag in task.tags %}
                            <span class="badge bg-light text-dark">{{ tag }}</span>
                        {% endfor %}
                    {% endif %}
                </div>
            </div>
            <div class="d-flex gap-2">
                <a href="{{ url_for('edit_task', task_id=task.id) }}" class="btn btn-primary">
                    <i class="fa fa-pencil mr-1"></i>编辑任务
                </a>
                <a href="{{ url_for('task_list') }}" class="btn btn-secondary">
                    <i class="fa fa-arrow-left mr-1"></i>返回列表
                </a>
            </div>
        </div>

        <!-- 任务详情卡片 -->
        <div class="row gap-4 mb-4">
            <!-- 左侧主要信息 -->
            <div class="col-lg-8">
                <!-- 任务描述卡片 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">任务描述</h4>
                    </div>
                    <div class="card-body">
                        {% if task.description %}
                            <p>{{ task.description }}</p>
                        {% else %}
                            <p class="text-muted">暂无任务描述</p>
                        {% endif %}
                    </div>
                </div>

                <!-- 任务状态更新卡片 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">更新任务状态</h4>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="{{ url_for('update_task_status', task_id=task.id) }}">
                            <div class="row gap-3">
                                <div class="col-md-6">
                                    <label for="status" class="form-label">任务状态</label>
                                    <select class="form-select" id="status" name="status">
                                        <option value="todo" {% if task.status == 'todo' %}selected{% endif %}>待办</option>
                                        <option value="in_progress" {% if task.status == 'in_progress' %}selected{% endif %}>进行中</option>
                                        <option value="completed" {% if task.status == 'completed' %}selected{% endif %}>已完成</option>
                                        <option value="archived" {% if task.status == 'archived' %}selected{% endif %}>已归档</option>
                                    </select>
                                </div>
                                <div class="col-md-6 align-self-end">
                                    <button type="submit" class="btn btn-primary w-100">
                                        <i class="fa fa-refresh mr-1"></i>更新状态
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 评论卡片 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">评论 ({{ comments | length }})</h4>
                    </div>
                    <div class="card-body">
                        <!-- 添加评论表单 -->
                        <form method="POST" action="{{ url_for('add_comment', task_id=task.id) }}" class="mb-4">
                            <div class="mb-2">
                                <textarea class="form-control" name="content" rows="3" placeholder="添加评论..." required></textarea>
                            </div>
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-comment mr-1"></i>提交评论
                            </button>
                        </form>

                        <!-- 评论列表 -->
                        {% if comments %}
                            {% for comment in comments %}
                                <div class="border-bottom pb-3 mb-3">
                                    <div class="d-flex justify-content-between">
                                        <div class="d-flex align-items-center gap-2">
                                            <img src="https://picsum.photos/id/{{ comment.user.id }}" alt="{{ comment.user.username }}" class="rounded-circle" width="32" height="32">
                                            <span class="font-weight-medium">{{ comment.user.username }}</span>
                                        </div>
                                        <span class="text-muted text-sm">{{ comment.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</span>
                                    </div>
                                    <p class="mt-2">{{ comment.content }}</p>
                                </div>
                            {% endfor %}
                        {% else %}
                            <p class="text-muted text-center py-3">暂无评论</p>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 右侧信息栏 -->
            <div class="col-lg-4">
                <!-- 任务信息卡片 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">任务信息</h4>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="text-muted d-block">创建时间</label>
                            <p>{{ task.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</p>
                        </div>
                        <div class="mb-3">
                            <label class="text-muted d-block">创建人</label>
                            <div class="d-flex align-items-center gap-2">
                                <img src="https://picsum.photos/id/{{ task.creator.id }}" alt="{{ task.creator.username }}" class="rounded-circle" width="24" height="24">
                                <span>{{ task.creator.username }}</span>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="text-muted d-block">负责人</label>
                            {% if task.assignee %}
                                <div class="d-flex align-items-center gap-2">
                                    <img src="https://picsum.photos/id/{{ task.assignee.id }}" alt="{{ task.assignee.username }}" class="rounded-circle" width="24" height="24">
                                    <span>{{ task.assignee.username }}</span>
                                </div>
                            {% else %}
                                <p class="text-muted">无负责人</p>
                            {% endif %}
                        </div>
                        <div class="mb-3">
                            <label class="text-muted d-block">截止日期</label>
                            {% if task.due_date %}
                                <p>
                                    {{ task.due_date.strftime('%Y-%m-%d %H:%M:%S') }}
                                    {% set days_left = (task.due_date - datetime.utcnow()).days %}
                                    {% if days_left < 1 and task.status != 'completed' and task.status != 'archived' %}
                                        <span class="text-danger ml-1">(今天)</span>
                                    {% elif days_left < 2 and task.status != 'completed' and task.status != 'archived' %}
                                        <span class="text-warning ml-1">(明天)</span>
                                    {% endif %}
                                </p>
                            {% else %}
                                <p class="text-muted">无截止日期</p>
                            {% endif %}
                        </div>
                        <div class="mb-3">
                            <label class="text-muted d-block">更新时间</label>
                            <p>{{ task.updated_at.strftime('%Y-%m-%d %H:%M:%S') }}</p>
                        </div>
                    </div>
                </div>

                <!-- 附件卡片 -->
                {% if task.attachment %}
                    <div class="card mb-4">
                        <div class="card-header bg-white border-bottom border-gray-200">
                            <h4 class="card-title mb-0">附件</h4>
                        </div>
                        <div class="card-body">
                            <a href="{{ url_for('static', filename='uploads/' ~ task.attachment) }}" target="_blank" class="d-flex align-items-center gap-2 p-3 bg-light rounded">
                                <i class="fa fa-file-o fa-2x text-primary"></i>
                                <div>
                                    <p class="font-medium mb-0">{{ task.attachment.split('/')[-1] }}</p>
                                    <p class="text-sm text-muted">{{ (task.attachment_size / 1024)|round(2) }} KB</p>
                                </div>
                            </a>
                        </div>
                    </div>
                {% endif %}

                <!-- 任务历史记录卡片 -->
                <div class="card">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">任务历史</h4>
                    </div>
                    <div class="card-body">
                        {% if task_history %}
                            <div class="list-group">
                                {% for history in task_history %}
                                    <div class="list-group-item border-0 pb-2 pt-2">
                                        <p class="text-sm">{{ history.changes }}</p>
                                        <p class="text-xs text-muted mt-1">{{ history.created_at.strftime('%Y-%m-%d %H:%M:%S') }} - {{ history.user.username }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                        {% else %}
                            <p class="text-muted text-center">暂无历史记录</p>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>
</body>
</html>